package com.customization.shunsheng;

import com.customization.shunsheng.util.LeaveNumberUtil;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.kq.cmd.balanceofleave.BatchProcessingCmd;
import com.engine.shunsheng.util.HandleForeignAnnualLeaveUtil;
import org.apache.commons.lang.StringUtils;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Map;

/**
 * 该拦截文件用于处理外籍类型的假期批处理时将该假期类型下的非外籍人员假期数据清空
 */
@CommandDynamicProxy(target = BatchProcessingCmd.class )
public class BatchProcessingCmdIntercept extends AbstractCommandProxy<Map<String,Object>> {

    BaseBean baseBean = new BaseBean();

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {
        BatchProcessingCmd cmd =(BatchProcessingCmd) command;
        User user = cmd.getUser();
        Map<String, Object> params = cmd.getParams();
        Map<String, Object> result = null;
        String wjnjid = baseBean.getPropValue("ss_prop_zm", "foreign_annual_leave_rule_id");
        String ruleId = Util.null2String(params.get("ruleId"));
        baseBean.writeLog("ruleId、wjnjid:" + ruleId + "、" + wjnjid);
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(ruleId) && org.apache.commons.lang3.StringUtils.equals(ruleId, wjnjid)) {
            baseBean.writeLog("start to handle foreign annual leave.");
            HandleForeignAnnualLeaveUtil handleForeignAnnualLeaveUtil = new HandleForeignAnnualLeaveUtil();
            handleForeignAnnualLeaveUtil.handleForeignAnnualLeave();
        }
        baseBean.writeLog("execute the original logic.");
        result = nextExecute(cmd);

        if ( StringUtils.isNotBlank(ruleId)) {
            LeaveNumberUtil leaveNumberUtil = new LeaveNumberUtil();
            leaveNumberUtil.clearForgResLeaveNumber(ruleId);
        }

        return result;
    }
}
